package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.my.R;
import com.my.util.ApiUtil;

public class LoginActivity extends AppCompatActivity {

    private EditText userId, password;
    SharedPreferences preferences = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        preferences = LoginActivity.this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        if(preferences.getString("user", null) != null) {
            startActivity(intent);
        }
        userId = findViewById(R.id.userId);
        password = findViewById(R.id.password);
        Button loginButton = findViewById(R.id.loginButton);
        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                JSONObject rest = ((JSONObject) msg.obj);
                if(rest.getInt("code") == 200) {
                    Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_LONG).show();
                    preferences.edit().putString("user", rest.getJSONObject("data").getStr("user")).commit();
                    preferences.edit().putString("setting", rest.getJSONObject("data").getStr("setting")).commit();
                    preferences.edit().putBoolean("absenteeismFirst", true).commit();
                    startActivity(intent);
                }else {
                    Toast.makeText(LoginActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
                }
            }
        };
        loginButton.setOnClickListener(v -> {
            if(StrUtil.isBlank(userId.getText()) || StrUtil.isBlank(password.getText())) {
                Toast.makeText(LoginActivity.this, "用户或密码不能为空！", Toast.LENGTH_LONG).show();
            }else if(!NumberUtil.isNumber(userId.getText())) {
                Toast.makeText(LoginActivity.this, "学号/工号只能包含数字！", Toast.LENGTH_LONG).show();
            }else {
                JSONObject params = new JSONObject(){{
                    putOpt("userId", userId.getText());
                    putOpt("password", password.getText());
                }};
                new Thread(() -> {
                    Message message = new Message();
                    message.obj = JSONUtil.parseObj(HttpUtil.post(ApiUtil.HTTP_ADDRESS + "login", params.toString()));
                    handler.sendMessage(message);
                }).start();
            }
        });
    }

    @Override
    protected void onRestart() {
        if(preferences.getString("user", null) != null) {
            onBackPressed();
            return;
        }
        super.onRestart();
    }
}